﻿using Sustainalytics.ClientDW.DataAccessLayer;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities;
using Sustainalytics.Entities.ConnectionStrings;
using Sustainalytics.Entities.ProductInvolvement;
using Sustainalytics.Entities.SustainableProducts;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Portfolios.BL;
using Sustainalytics.ScreeningTool.BL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;

namespace Sustainalytics.PregenerateScreeningReports.Service
{
    public class ScreeningToolHelper
    {
        Dictionary<ConnectionStringType, string> _connectionStringsMap;
        string _redisServerAddress;
        string _ESGComputeURL;
        private ExportScreeningEx _exportBL;

        public ScreeningToolHelper(Dictionary<ConnectionStringType, string> connectionStringsMap,
                                   string redisServerAddress,
                                   string ESGComputeURL = null)
        {
            _connectionStringsMap = connectionStringsMap;
            _redisServerAddress = redisServerAddress;
            _ESGComputeURL = ESGComputeURL;
            var screeningDataRepository = ScreeningDataRepository.GetScreeningDataRepository(_connectionStringsMap[ConnectionStringType.ScreeningDW]);

            var portfolioManager = new PortfolioReadHandler(new PortfolioTypeCollection<Portfolio>(new RepositoryCollection<Portfolio>(_connectionStringsMap[ConnectionStringType.ClientPlatform]), new RepositoryCollection<Portfolio>(_connectionStringsMap[ConnectionStringType.ClientPlatform])),
                                           new PortfolioTypeCollection<PortfolioDetails>(new RepositoryCollection<PortfolioDetails>(_connectionStringsMap[ConnectionStringType.ClientPlatform]), new RepositoryCollection<PortfolioDetails>(_connectionStringsMap[ConnectionStringType.ClientPlatform])),
                                           new RepositoryCollection<CorporateDataBasic>(_connectionStringsMap[ConnectionStringType.ClientDW], "CorporateData"));


            _exportBL = new ExportScreeningEx(CompanyScreeningRepository.GetCompanyScreeningRepository(_connectionStringsMap[ConnectionStringType.ClientPlatform]),
                                                ClientDwRepository.GetInstance(_connectionStringsMap[ConnectionStringType.ClientDW]),
                                                portfolioManager,
                                                UniversesRepository.GetUniversesRepository(_connectionStringsMap[ConnectionStringType.ClientUniverses]),
                                                new FilterCriteriaRepository(_connectionStringsMap[ConnectionStringType.ClientPlatform]),
                                                new RepositoryCollection<CorporateData>(_connectionStringsMap[ConnectionStringType.ClientDW]),
                                                screeningDataRepository,
                MongoFactory.GetCollectionFromDatabase<CorporateData>(MongoFactory.GetConnectionStrings().ClientDW),
                MongoFactory.GetCollectionFromDatabase<SustainableProductsFramework>(MongoFactory.GetConnectionStrings().ProductInvolvementDW),
                MongoFactory.GetCollectionFromDatabase<ProductInvolvementFramework>(MongoFactory.GetConnectionStrings().ProductInvolvementDW));
        }

        public void SaveMaximalReport(Guid filterId, string fileName)
        {




            var content = _exportBL.RunScreeningExportAllAccounts(filterId, ExtractESGComputationUrl, _connectionStringsMap[ConnectionStringType.ClientDW]);
            File.WriteAllBytes(fileName, content);
        }

        public string ExtractESGComputationUrl()
        {
            var localUrl = (string)ConfigurationManager.AppSettings["ESGComputeURL"];
            if (localUrl == null)
                localUrl = "http://localhost:10280/esgcomputingserv/api/v1/esgcompute";

            return localUrl;

        }

    }
}
